<template>
	<div class="system-dept-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="deptDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="state.rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级部门">
							<el-cascader :options="state.deptData"
								:props="{ checkStrictly: true, value: 'deptId', label: 'deptName' }" placeholder="请选择部门"
								clearable class="w100" v-model="state.ruleForm.parentId">
								<template #default="{ node, data }">
									<span>{{ data.deptName }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门名称" prop="deptName">
							<el-input v-model="state.ruleForm.deptName" placeholder="请输入部门名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="负责人" prop="leader">
							<el-input v-model="state.ruleForm.leader" placeholder="请输入负责人" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号" prop="phone">
							<el-input v-model="state.ruleForm.phone" placeholder="请输入手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="邮箱" prop="email">
							<el-input v-model="state.ruleForm.email" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序" prop="orderNum">
							<el-input-number v-model="state.ruleForm.orderNum" :min="0" :max="999" controls-position="right"
								placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门状态">
							<el-radio-group v-model="state.ruleForm.status">
								<el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{
									dict.label }}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default" v-auth="'system:dept:edit'">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDeptDialog">
import { reactive, ref } from 'vue';
import { useDept } from '/@/api/system/dept';
import { ElMessage } from 'element-plus';
import { useDict } from '/@/utils/dict';

const { sys_normal_disable } = useDict("sys_normal_disable");

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const deptDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		createBy: '',
		createTime: '',
		updateBy: '',
		updateTime: '',
		parentName: '',
		parentId: 100,
		children: [],
		deptId: 0,
		deptName: '',
		orderNum: 0,
		leader: '',
		phone: '',
		email: '',
		status: '0',
		delFlag: '0',
		ancestors: '',
	},
	deptData: {
		createBy: '',
		createTime: '',
		updateBy: '',
		updateTime: '',
		parentName: '',
		parentId: 0,
		children: [],
		deptId: 0,
		deptName: '',
		orderNum: 0,
		leader: '',
		phone: '',
		email: '',
		status: '0',
		delFlag: '0',
		ancestors: '',
	},
	rules: {
		deptName: [
			{ required: true, message: '请输入部门名称', trigger: 'blur' },
			{ min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
		],
		leader: [
			{ required: true, message: '请输入负责人', trigger: 'blur' },
			{ min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
		],
		phone: [
			{ required: true, message: '请输入手机号', trigger: 'blur' },
			{ min: 11, max: 11, message: '长度在 11 个字符', trigger: 'blur' },
		],
		email: [
			{ required: true, message: '请输入邮箱', trigger: 'blur' },
			{ type: 'email', message: '请输入正确的邮箱', trigger: 'blur' },
		],
		orderNum: [
			{ required: true, message: '请输入排序', trigger: 'blur' },
			{ type: 'number', message: '请输入数字', trigger: 'blur' },
		],

	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, deptTree: RowDeptType, row: RowDeptType) => {
	state.deptData = deptTree;
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改部门';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增部门';
		state.dialog.submitTxt = '新 增';
		getDeptRuleForm();
	}
	state.dialog.isShowDialog = true;
};

// 提交
const onSubmit = async () => {
	try {
		await deptDialogFormRef.value.validate(async (valid: any, fields: any) => {
			if (valid) {
				if (state.dialog.type === 'add') {
					await addDept();
				} else {
					await updateDept();
				}
			} else {
				return Promise.reject(fields);
			}
		});
	} catch (err) {
		Promise.reject(err);
	}
};

const addDept = async () => {
	const res = await useDept().addDept({
		deptName: state.ruleForm.deptName,
		parentId: state.ruleForm.parentId,
		orderNum: state.ruleForm.orderNum,
		leader: state.ruleForm.leader,
		phone: state.ruleForm.phone,
		email: state.ruleForm.email,
		status: state.ruleForm.status,
	});
	if (res.code === 200) {
		ElMessage.success(res.msg);
		closeDialog();
		emit('refresh');
	} else {
		ElMessage.error(res.msg);
	}
};

const updateDept = async () => {
	const res = await useDept().updateDept(state.ruleForm);
	if (res.code === 200) {
		ElMessage.success(res.msg);
		closeDialog();
		emit('refresh');
	} else {
		ElMessage.error(res.msg);
	}
};





// 初始化部门数据
const getDeptRuleForm = () => {
	state.ruleForm = {
		createBy: '',
		createTime: '',
		updateBy: '',
		updateTime: '',
		parentName: '',
		parentId: 100,
		children: [],
		deptId: 0,
		deptName: '',
		orderNum: 0,
		leader: '',
		phone: '',
		email: '',
		status: '0',
		delFlag: '0',
		ancestors: '',
	};
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};// 暴露变量
defineExpose({
	openDialog,
});
</script>